﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Eye.Logic;

namespace The_Eye
{
    public partial class mainForm : Form
    {
        Recognizing recognizing = new Recognizing();
        QuestionForm supportForm = null;
        DBNotes notes = new DBNotes();

        public mainForm()
        {
            InitializeComponent();
            setEnabledOfFormElements(false);
            btnRecognize.Enabled = false;

        }

        private void imageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnRecognize.Enabled = false;
            setEnabledOfFormElements(false);
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                if ((theEyePB.Image = Bitmap.FromFile(openFileDialog.FileName)) != null)
                {
                    if (openFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        if ((theIrisPB.Image = Bitmap.FromFile(openFileDialog.FileName)) != null)
                        {
                            btnRecognize.Enabled = true;
                        }
                    }
                }
            }
        }


        public void setEnabledOfFormElements(bool value)
        {
            //configure buttons
            btnRecognize.Enabled = !value;
            btnAddToDB.Enabled = value;
            //configure Text Boxes
            tbName.Enabled = value;
            //pb .Enabled = value;
            tbSecondName.Enabled = value;
            tbMidlName.Enabled = value;
            tbPassportNumber.Enabled = value;
        }

        private void btnRecognize_Click(object sender, EventArgs e)
        {
            if (recognizing.Recognize(new Bitmap(theIrisPB.Image)))
            {
            }
            else
            {
                supportForm = new QuestionForm("Recognizing failed", "This user was not in DB.",
                                                "Would you like to add him?", true, true, this);
                supportForm.ShowDialog();
            }
        }

        private void btnAddToDB_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(tbName.Text) &&
               !String.IsNullOrEmpty(tbSecondName.Text) && !String.IsNullOrEmpty(tbMidlName.Text) &&
               !String.IsNullOrEmpty(tbPassportNumber.Text) && theIrisPB.Image != null)
            {
                User user = new User()
                {
                    First_Name = tbName.Text,
                    Second_Name = tbSecondName.Text,
                    Midl_Name = tbMidlName.Text,
                    Passport = tbPassportNumber.Text,
                    TheEyeImage = ConvertImage(theEyePB.Image),
                    TheIrisImage = ConvertImage(theIrisPB.Image),
                    FirstCode = null, //Insert bitmap of code
                    SecondCode = null// //-//
                };
                UsersService.Save(user);

                MessageBox.Show("Date added successfuly");
            }
            else
            {
                MessageBox.Show("One or more text fields are not filled");
            }
        }

        private void viewAllRecordsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DBNotes notesForm = new DBNotes();
            notesForm.ShowDialog();
        }

        private byte[] ConvertImage(System.Drawing.Image img)
        {
            ImageConverter converter = new ImageConverter();
            return (byte[])converter.ConvertTo(img, typeof(byte[]));
        }
    }
}
